/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.wujingchao.ohos.view.slice;

import net.wujingchao.ohos.view.MultiCardMenu;
import net.wujingchao.ohos.view.ResourceTable;
import net.wujingchao.ohos.view.LogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Switch;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;

/**
 * Main ability slice
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    /**
     * * The constant TAG
     */
    private static final String TAG = "MainAbilitySlice";
    /**
     * The constant List container
     */
    private ListContainer listContainer;
    /**
     * The constant Page slider
     */
    private PageSlider pageSlider;
    /**
     * The constant Multi card menu
     */
    private MultiCardMenu multiCardMenu;
    /**
     * The constant Data
     */
    String[] data = {"mother",
            "passion",
            "smile",
            "love",
            "eternity",
            "fantastic",
            "destiny",
            "freedom",
            "liberty",
            "tranquility",
            "peace",
            "blossom",
            "sunshine",
            "sweetheart",
            "gorgeous",
            "cherish",
            "enthusiasm",
            "hope",
            "grace",
            "rainbow",
            "blue",
            "sunflower",
            "twinkle",
            "serendipity",
            "bliss",
            "lullaby",
            "sophisticated",
            "renaissance",
            "cute",
            "cosy",
            "butterfly",
            "galaxy",
            "hilarious",
            "moment",
            "extravaganza"};
    private BaseItemProvider baseItemProvider = new BaseItemProvider() {
        @Override
        public int getCount() {
            return data.length;
        }

        @Override
        public Object getItem(int position) {
            return data[position];
        }

        @Override
        public long getItemId(int posititon) {
            return posititon;
        }

        private SettingHolder holder;

        @Override
        public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
            final Component cpt;
            if (component == null) {
                cpt = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_item_list, null, false);
                holder = new SettingHolder(cpt);
                cpt.setTag(holder);
            } else {
                cpt = component;
                holder = (SettingHolder) cpt.getTag();
            }


            holder.settingText.setText(data[position]);
            return cpt;
        }
    class SettingHolder {
        Image settingIma;
        Text settingText;
        Switch settingSwitch;

        SettingHolder(Component component) {
            settingText = (Text) component.findComponentById(ResourceTable.Id_item_index);
        }
    }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
        setProvider();
    }

    private void setProvider() {
        int[] imageRes = new int[]{ResourceTable.Media_eff, ResourceTable.Media_ent,
                ResourceTable.Media_nuan1, ResourceTable.Media_nuan2, ResourceTable.Media_qa,
                ResourceTable.Media_eff, ResourceTable.Media_ent, ResourceTable.Media_nuan1,
                ResourceTable.Media_nuan2, ResourceTable.Media_qa};
        pageSlider.setProvider(new PageSliderProvider() {
            @Override
            public int getCount() {
                return imageRes.length;
            }

            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int position) {
                Image image = new Image(getContext());
                image.setImageAndDecodeBounds(imageRes[position]);
                image.setLayoutConfig(new StackLayout.LayoutConfig(
                        ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT));
                image.setScaleMode(Image.ScaleMode.CLIP_CENTER);
                componentContainer.addComponent(image);
                return image;
            }

            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object obiect) {
                componentContainer.removeComponent((Component) obiect);
            }

            @Override
            public boolean isPageMatchToObject(Component component, Object object) {
                return true;
            }
        });
        listContainer.setItemProvider(baseItemProvider);
    }

    private void initView() {
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_lc_main);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_ps_main);
        multiCardMenu = (MultiCardMenu) findComponentById(ResourceTable.Id_multi_card_menu);
        multiCardMenu.setBackgroundRid(ResourceTable.Layout_background_view);
        multiCardMenu.setOnDisplayOrHideListener(new MultiCardMenu.OnDisplayOrHideListener() {
            @Override
            public void onDisplay(int which) {
                LogUtil.info(TAG, "onDisplay:" + which);
            }

            @Override
            public void onHide(int which) {
                LogUtil.info(TAG, "onHide:" + which);
            }

            @Override
            public void onTouchCard(int which) {
                LogUtil.info(TAG, "onTouchCard:" + which);
            }
        });
        findComponentById(ResourceTable.Id_btn_bg_one).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_bg_two).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_bg_three).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_bg_four).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_bg_fail).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_bg_blue).setClickedListener(this);
        Checkbox boundary = (Checkbox) findComponentById(ResourceTable.Id_cb_bg_boundary);
        Checkbox fade = (Checkbox) findComponentById(ResourceTable.Id_cb_bg_fade);
        boundary.setChecked(true);
        fade.setChecked(true);
        boundary.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                multiCardMenu.setBoundary(isChecked);
            }
        });
        fade.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                multiCardMenu.setFade(isChecked);
            }
        });
    }

    /**
     * On active
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * On foreground *
     *
     * @param intent intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * On click *
     *
     * @param component component
     */
    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btn_bg_one:
                multiCardMenu.show(0);
                break;
            case ResourceTable.Id_btn_bg_two:
                multiCardMenu.show(1);
                break;
            case ResourceTable.Id_btn_bg_three:
                multiCardMenu.show(2);
                break;
            case ResourceTable.Id_btn_bg_four:
                multiCardMenu.show(3);
                break;
            case ResourceTable.Id_btn_bg_fail:
                ToastDialog toastDialog = new ToastDialog(getContext());
                toastDialog.setText("Fail").show();
                break;
            case ResourceTable.Id_btn_bg_blue:
                ToastDialog toastDialog1 = new ToastDialog(getContext());
                toastDialog1.setText("Blue").show();
                break;
        }
    }
}
